<?php 

/*
 * othAuth By othman ouahbi.
 * comments, bug reports are welcome crazylegs@gmail.com
 * @author CraZyLeGs
 * @version 0.1.0 
 * @license MIT
 */
class othAuthHelper extends Helper
{
    //var $helpers = array('Html','Javascript');
    

    var $initialized = false;
    
    
    
    function init()
    {
        
        
        if(!$this->initialized)
        {
            
            if(!empty($this->view->_viewVars['othAuth_data']))
            {
                $data = $this->view->_viewVars['othAuth_data'];
                
                foreach($data as $k => $v)
                {
                    $this->$k = $v;
                }
                
                //$this->us = 'othAuth.'.$this->hashkey;
            }
            $this->initialized = true;
        }

    }
    
    function sessionValid()
    {
        $this->init();
        return (!empty($_SESSION['othAuth'][$this->hashkey]));
    }
    // helper methods
    function user($arg)
    {
        $this->init();
        // does session exists
        if($this->sessionValid())
        {
            $ses = $_SESSION['othAuth'][$this->hashkey];
            if(isset($ses["{$this->user_model}"][$arg]))
            {
                return $ses["{$this->user_model}"][$arg];
            }
            else
            {
                return false;
            }
        }
        return false;    
        
        
        //return $this->view->controller->othAuth->user($arg);
    }
    
    // helper methods
    function group($arg)
    {
        
        $this->init();
        // does session exists
        if($this->sessionValid())
        {
            $ses = $_SESSION['othAuth'][$this->hashkey];
            if(isset($ses["{$this->group_model}"][$arg]))
            {
                return $ses["{$this->group_model}"][$arg];
            }
            else
            {
                return false;
            }
        }
        return false;
        
        
        //return $this->view->controller->othAuth->group($arg);
    }
    
    
    // helper methods
    function permission($arg)
    {
        
        $this->init();
        // does session exists
        if($this->sessionValid())
        {
            $ses = $_SESSION['othAuth'][$this->hashkey];
            if(isset($ses[$this->group_model][$this->permission_model]))
            {
                $ret = array();
                if(is_array($ses[$this->group_model][$this->permission_model]))
                {
                    for($i = 0; $i < count($ses[$this->group_model][$this->permission_model]); $i++ )
                    {
                        $ret[] = $ses[$this->group_model][$this->permission_model][$i][$arg];    
                    }
                }
                return $ret;
            }
            else
            {
                return false;
            }
        }
        return false;    
        
        //return $this->view->controller->othAuth->permission($arg);
    }
    
    function getData($arg = '')
    {
        
        $this->init();
        // does session exists
        if($this->sessionValid())
        {
            
            return $_SESSION['othAuth'][$this->hashkey];
        }
        return false;    
        
        //return $this->view->controller->othAuth->getData($arg);
    }
    
}
?>